package cc.linker.steplink.crm.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.dao.ScoreActDao;
import cc.linker.steplink.crm.merchanth5.service.acs.DynamicService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import cc.linker.steplink.crm.service.IScoreActService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/scoreact")
public class ScoreActiveController {


    @Autowired
    private IScoreActService scoreActService;

    @Autowired
    private ScoreActDao scoreactdao;

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    @PostMapping("/list")
    public Object list(@RequestBody Map<String, Object> map, HttpServletRequest request) throws GlobalException {
        if(StringUtils.isEmpty(map.get("type"))){
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        int pn = 1, pageSize = 10;
        Object pno = map.get("pn");
        if (pno instanceof Integer && !StringUtils.isEmpty(pno)) {
            pn = (int) pno;
        }
        Object pageSizeo = map.get("pageSize");
        if (pageSizeo instanceof Integer && !StringUtils.isEmpty(pageSizeo)) {
            pageSize = (int) pageSizeo;
        }
        String orgId = userinfo.getOrgId();
        map.put("orgId", orgId);
        int count = scoreActService.countScoreActBase(map);
        if(count > 0){
            if(count == 5){
                this.share(orgId,(String)map.get("type"));
            }
            PageHelper.startPage(pn, pageSize);
            List<Map<String, Object>> list = scoreActService.listScoreAct(map);
            return new PageInfo<>(list);
        }else{
            return new PageInfo<>(this.baselist(orgId,(String)map.get("type")));
        }
    }


    private void share(String orgId,String type){
        Map<String, String> shareopen = new HashMap<>();
        shareopen.put("orgId", orgId);
        shareopen.put("scoreactId", UUID.randomUUID().toString());
        shareopen.put("scoreactName", "分享传播");
        shareopen.put("scoreactType", "share");
        shareopen.put("activeType", "open");
        shareopen.put("status", "0");
        shareopen.put("type", type);
        Map<String, String> ruleopen = new HashMap<>();
        ruleopen.put("val1", "");
        ruleopen.put("val2", "");
        shareopen.put("rule", JSON.toJSONString(ruleopen));
        scoreactdao.addScoreAct(shareopen);

        Map<String, String> sharesubscribe = new HashMap<>();
        sharesubscribe.put("orgId", orgId);
        sharesubscribe.put("scoreactId", UUID.randomUUID().toString());
        sharesubscribe.put("scoreactName", "分享关注公众号");
        sharesubscribe.put("scoreactType", "share");
        sharesubscribe.put("activeType", "subscribe");
        sharesubscribe.put("status", "0");
        sharesubscribe.put("type", type);
        Map<String, String> rulesubscribe = new HashMap<>();
        rulesubscribe.put("val1", "");
        rulesubscribe.put("val2", "");
        sharesubscribe.put("rule", JSON.toJSONString(rulesubscribe));
        scoreactdao.addScoreAct(sharesubscribe);

        Map<String, String> shareregister = new HashMap<>();
        shareregister.put("orgId", orgId);
        shareregister.put("scoreactId", UUID.randomUUID().toString());
        shareregister.put("scoreactName", "邀请注册");
        shareregister.put("scoreactType", "share");
        shareregister.put("activeType", "register");
        shareregister.put("status", "0");
        shareregister.put("type", type);
        Map<String, String> ruleregister = new HashMap<>();
        ruleregister.put("val1", "");
        ruleregister.put("val2", "");
        shareregister.put("rule", JSON.toJSONString(ruleregister));
        scoreactdao.addScoreAct(shareregister);

        Map<String, String> sharebuy = new HashMap<>();
        sharebuy.put("orgId", orgId);
        sharebuy.put("scoreactId", UUID.randomUUID().toString());
        sharebuy.put("scoreactName", "分享成交");
        sharebuy.put("scoreactType", "share");
        sharebuy.put("activeType", "deal");
        sharebuy.put("status", "0");
        sharebuy.put("type", type);
        Map<String, String> rulebuy = new HashMap<>();
        rulebuy.put("val1", "");
        rulebuy.put("val2", "");
        sharebuy.put("rule", JSON.toJSONString(rulebuy));
        scoreactdao.addScoreAct(sharebuy);
    }


    private List<Map> baselist(String orgId,String type){

        Map<String, String> shareopen = new HashMap<>();
        shareopen.put("orgId", orgId);
        shareopen.put("scoreactId", UUID.randomUUID().toString());
        shareopen.put("scoreactName", "分享传播");
        shareopen.put("scoreactType", "share");
        shareopen.put("activeType", "open");
        shareopen.put("status", "0");
        shareopen.put("type", type);
        Map<String, String> ruleopen = new HashMap<>();
        ruleopen.put("val1", "");
        ruleopen.put("val2", "");
        shareopen.put("rule", JSON.toJSONString(ruleopen));
        scoreactdao.addScoreAct(shareopen);

        Map<String, String> sharesubscribe = new HashMap<>();
        sharesubscribe.put("orgId", orgId);
        sharesubscribe.put("scoreactId", UUID.randomUUID().toString());
        sharesubscribe.put("scoreactName", "分享关注公众号");
        sharesubscribe.put("scoreactType", "share");
        sharesubscribe.put("activeType", "subscribe");
        sharesubscribe.put("status", "0");
        sharesubscribe.put("type", type);
        Map<String, String> rulesubscribe = new HashMap<>();
        rulesubscribe.put("val1", "");
        rulesubscribe.put("val2", "");
        sharesubscribe.put("rule", JSON.toJSONString(rulesubscribe));
        scoreactdao.addScoreAct(sharesubscribe);

        Map<String, String> shareregister = new HashMap<>();
        shareregister.put("orgId", orgId);
        shareregister.put("scoreactId", UUID.randomUUID().toString());
        shareregister.put("scoreactName", "邀请注册");
        shareregister.put("scoreactType", "share");
        shareregister.put("activeType", "register");
        shareregister.put("status", "0");
        shareregister.put("type", type);
        Map<String, String> ruleregister = new HashMap<>();
        ruleregister.put("val1", "");
        ruleregister.put("val2", "");
        shareregister.put("rule", JSON.toJSONString(ruleregister));
        scoreactdao.addScoreAct(shareregister);

        Map<String, String> sharebuy = new HashMap<>();
        sharebuy.put("orgId", orgId);
        sharebuy.put("scoreactId", UUID.randomUUID().toString());
        sharebuy.put("scoreactName", "分享成交");
        sharebuy.put("scoreactType", "share");
        sharebuy.put("activeType", "deal");
        sharebuy.put("status", "0");
        sharebuy.put("type", type);
        Map<String, String> rulesbuy = new HashMap<>();
        rulesbuy.put("val1", "");
        rulesbuy.put("val2", "");
        sharebuy.put("rule", JSON.toJSONString(rulesbuy));
        scoreactdao.addScoreAct(sharebuy);

        Map<String, String> baseshare = new HashMap<String, String>();
        baseshare.put("orgId", orgId);
        baseshare.put("scoreactId", UUID.randomUUID().toString());
        baseshare.put("scoreactName", "转发分享");
        baseshare.put("scoreactType", "news");
        baseshare.put("activeType", "share");
        baseshare.put("status", "0");
        baseshare.put("type", type);
        Map<String, String> ruleshare = new HashMap<String, String>();
        ruleshare.put("val1", "");
        ruleshare.put("val2", "");
        JSONObject jsonObjectShare = (JSONObject) JSONArray.toJSON(ruleshare);
        baseshare.put("rule", jsonObjectShare.toJSONString());
        scoreactdao.addScoreAct(baseshare);

        Map<String, String> basesgivestar = new HashMap<String, String>();
        basesgivestar.put("orgId", orgId);
        basesgivestar.put("scoreactId", UUID.randomUUID().toString());
        basesgivestar.put("scoreactName", "文章点赞");
        basesgivestar.put("scoreactType", "news");
        basesgivestar.put("activeType", "givestar");
        basesgivestar.put("status", "0");
        basesgivestar.put("type", type);
        Map<String, String> rulegivestar = new HashMap<String, String>();
        rulegivestar.put("val1", "");
        rulegivestar.put("val2", "");
        JSONObject jsonObjectGivestar = (JSONObject) JSONArray.toJSON(rulegivestar);
        basesgivestar.put("rule", jsonObjectGivestar.toJSONString());
        scoreactdao.addScoreAct(basesgivestar);

        Map<String, String> basecomment = new HashMap<String, String>();
        basecomment.put("orgId", orgId);
        basecomment.put("scoreactId", UUID.randomUUID().toString());
        basecomment.put("scoreactName", "文章评论");
        basecomment.put("scoreactType", "news");
        basecomment.put("activeType", "comment");
        basecomment.put("status", "0");
        basecomment.put("type", type);
        Map<String, String> rulecomment = new HashMap<String, String>();
        rulecomment.put("val1", "");
        rulecomment.put("val2", "");
        JSONObject jsonObjectComment = (JSONObject) JSONArray.toJSON(rulecomment);
        basecomment.put("rule", jsonObjectComment.toJSONString());
        scoreactdao.addScoreAct(basecomment);

        //购买商品
        Map<String, String> basebuy = new HashMap<String, String>();
        basebuy.put("orgId", orgId);
        basebuy.put("scoreactId", UUID.randomUUID().toString());
        basebuy.put("scoreactName", "购买商品");
        basebuy.put("scoreactType", "shopping");
        basebuy.put("activeType", "buy");
        basebuy.put("status", "0");
        basebuy.put("type", type);
        Map<String, String> rulebuy = new HashMap<String, String>();
        rulebuy.put("val1", "");
        rulebuy.put("val2", "");
        basebuy.put("rule", JSON.toJSONString(rulebuy));
        scoreactdao.addScoreAct(basebuy);

        //完成下单
        Map<String, String> baseorder = new HashMap<String, String>();
        baseorder.put("orgId", orgId);
        baseorder.put("scoreactId", UUID.randomUUID().toString());
        baseorder.put("scoreactName", "完成下单");
        baseorder.put("scoreactType", "shopping");
        baseorder.put("activeType", "order");
        baseorder.put("status", "0");
        baseorder.put("type", type);
        Map<String, String> ruleorder = new HashMap<String, String>();
        ruleorder.put("val1", "");
        ruleorder.put("val2", "");
        baseorder.put("rule", JSON.toJSONString(ruleorder));
        scoreactdao.addScoreAct(baseorder);


        List<Map> result = new ArrayList<>();
        result.add(basebuy);
        result.add(baseorder);
        result.add(shareopen);
        result.add(sharebuy);
        result.add(shareregister);
        result.add(sharesubscribe);
        result.add(baseshare);
        result.add(basesgivestar);
        result.add(basecomment);

        return result;
    }

    @PostMapping("/save")
    public void save(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId",orgId);
        if(StringUtils.isEmpty(map.get("type")) || null == map.get("rule")){
            throw GlobalException.build("参数错误");
        }
        map.put("rule",JSON.toJSONString(map.get("rule"), SerializerFeature.WriteMapNullValue));
        if(!StringUtils.isEmpty(map.get("scoreactId"))){
            HashMap<String, Object> scoreactId = scoreActService.getScoreActInfo(map.get("scoreactId").toString());
            if("1".equals(scoreactId.get("type"))){
                dynamicService.recording(request, 15,"调整了积分任务");
            }else{
                dynamicService.recording(request, 17,"调整了成长任务");
            }
            scoreActService.editScoreAct(map);
        }else{
            map.put("scoreactId", UUID.randomUUID().toString());
            map.put("scoreactName",map.get("scoreactName"));
            map.put("scoreactType","custom");
            map.put("activeType","custom");
            map.put("status", "0");
            scoreActService.addScoreAct(map);
        }
    }

    @GetMapping("/getgoodsids")
    public Map<String,Object> getGoodsIds(String type, HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Map<String,Object> param = new HashMap<>();
        param.put("orgId",orgId);
        param.put("type",type);
        return scoreActService.getGoodsIds(param);
    }

    @GetMapping("/info")
    public Map<String,Object> info(String scoreactId){
        Map<String, Object> info = scoreActService.getInfo(scoreactId);
        List<Map<String,Object>> rule = (List<Map<String,Object>>)JSON.parse(info.get("rule").toString());
        for(Map<String,Object> r:rule){
            String goodsId = r.get("goodsId").toString();
            Map<String,Object> goodsInfo = scoreActService.getGoods(goodsId);
            r.put("goodsName",goodsInfo.get("goodsName"));
            r.put("goodsCover",goodsInfo.get("goodsCover"));
            r.put("amount",goodsInfo.get("amount"));
        }
        info.put("rule",rule);
        return info;
    }

    @PostMapping("/changestatus")
    public void changeStatus(@RequestBody Map<String, Object> map){
        scoreActService.changeStatus(map);
    }

    @PostMapping("/add")
    public void add(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId",orgId);
        map.put("scoreactId", UUID.randomUUID().toString());
        map.put("scoreactName",map.get("scoreactName"));
        map.put("scoreactType","custom");
        map.put("activeType","custom");
        map.put("status", "0");
        map.put("rule", StringUtils.isEmpty(map.get("rule")) ? "" : map.get("rule"));
        scoreActService.addScoreAct(map);
    }

    @PostMapping("/edit")
    public void edit(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        if (StringUtils.isEmpty(map.get("scoreactId"))) {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
        HashMap<String, Object> scoreactId = scoreActService.getScoreActInfo(map.get("scoreactId").toString());
        if("1".equals(scoreactId.get("type"))){
            dynamicService.recording(request, 15,"调整了积分任务");
        }else{
            dynamicService.recording(request, 17,"调整了成长任务");
        }
        map.put("rule", StringUtils.isEmpty(map.get("rule")) ? "" : map.get("rule"));
        scoreActService.editScoreAct(map);
    }


    @PostMapping("/del")
    public void del(@RequestBody Map<String, Object> map) throws Exception {
        if (StringUtils.isEmpty(map.get("scoreactId"))) {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
        String scoreActId = map.get("scoreactId").toString();
        scoreActService.delScoreAct(scoreActId);
    }

    @PostMapping("/getinfo")
    public Object getInfo(HttpServletRequest request, @RequestBody Map<String, Object> map) throws Exception {
        if (StringUtils.isEmpty(map.get("scoreactId"))) {
            throw new GlobalException(ResponseCode.ID_NULL);
        }
        String scoreActId = map.get("scoreactId").toString();
        HashMap<String, Object> scoreActInfo = scoreActService.getScoreActInfo(scoreActId);
        if (scoreActInfo == null) {
            throw GlobalException.build("参数错误");
        }
        return scoreActInfo;
    }


}
